<?php

class DriversController extends Controller {

    public function actionIndex() {
        $c = new CDbCriteria();
        $c->order = 'evstat_id desc';
        $models = Evstats::model()->findAll();
        $evakID = isset($_GET['evakID']) ? $_GET['evakID'] : Evaks::model()->find('evak_user = \'' . Yii::app()->user->id . '\' order by evak_id')->evak_id;
        $logs = EvakLog::model()->findAll('evak_id = \'' . (int) $evakID . '\'');

        $sql = 'SELECT
                        s.evstat_payable,
                        l.status_log_begin,
                        l.status_log_end,
                        l.evak_status_id,	
                        c.city_price
                FROM
                        ev_users u
                JOIN ev_evaks e ON u.user_id = e.evak_user :: INT
                JOIN ev_evak_log l ON e.evak_id = l.evak_id :: INT
                JOIN ev_evstats s ON l.evak_status_id = s.evstat_id
                JOIN ev_cities C ON e.evak_city = c.city_id
                WHERE
                        s.evstat_payable = 1
                AND l.evak_id :: INT = ' . (int) $evakID . ' ORDER BY l.status_log_begin DESC';
        $data = array();
        $statuses = Yii::app()->db->getCommandBuilder()->createSqlCommand($sql)->queryAll();
        foreach ($statuses as $status) {
            $data[date('d.m.Y', $status['status_log_begin'])][] = $status;
        }

        $this->render('index', array('models' => $models, 'logs' => $logs, 'evakID' => $evakID, 'data' => $data));
    }

}
